<?php

namespace App\Repos;

use FFMpeg\FFMpeg;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\Format\Video\X264;

class FFMpeger
{

    protected $path = [];

    protected $ffmpeg;

    protected $targetPath;

    protected $newPath;

    public function __construct()
    {
        $this->init();
    }

    private function init()
    {
        if (empty($this->ffmpeg)) {
            $ffmpeg =  '/usr/bin/ffmpeg';
            $ffprobe = '/usr/bin/ffprobe';
            if(empty($ffmpeg) && empty($ffprobe)){
                $this->path = [];
            }else{
                $this->path = [
                    // 'ffmpeg.binaries'  => 'D:\ffmpeg\bin\avconv',
                    'ffmpeg.binaries' => $ffmpeg,
                    // 'ffprobe.binaries' => 'D:\ffmpeg\bin\avprobe',
                    'ffprobe.binaries' => $ffprobe
                    // 'timeout'          => 3600, // The timeout for the underlying process
                    // 'ffmpeg.threads'   => 12,   // The number of threads that FFMpeg should us
                ];
            }
            // dump($this->path);exit;
            $this->ffmpeg = FFMpeg::create($this->path);
        }
    }

    public function setTargetPath($path){
        $this->targetPath = $path;
        $this->createDir();
        $this->setNewPath();
        return $this;
    }

    public function setNewPath(){
        $now = date('YmdHis') . '_' . bin2hex(random_bytes(10));
        //自动添加一层目录
        $catalog = dirname($this->targetPath) . DIRECTORY_SEPARATOR . $now;
        if (!is_dir($catalog)) {
            mkdir($catalog);
        }
        $newPath = $catalog . strrchr($this->targetPath, DIRECTORY_SEPARATOR);
        $this->newPath = $newPath;
    }


    public function createDir()
    {
        if (!is_dir(dirname($this->targetPath))) {
            $this->createDir(dirname($this->targetPath));
            mkdir(dirname($this->targetPath));
        }
    }

    //拼接视频、音频
    public function montage($path1, $path2)
    {
        $video = $this->ffmpeg->open($path1);
        $video->concat(array($path1, $path2))->saveFromSameCodecs($this->newPath);
        return $this->newPath;
    }
    //视频截图
    public function videoScreenShot($videoPath, $second = 1)
    {
        $video = $this->ffmpeg->open($videoPath);
        $frame = $video->frame(TimeCode::fromSeconds($second));
        $frame->save($this->newPath);
        return $this->newPath;
    }
    //视频添加水印
    public function videoWatermark($videoPath, $watermarkPath)
    {
        $video = $this->ffmpeg->open($videoPath);
        $absolute = ['x' => 50, 'y' => 100];
        $relative = [
            'position' => 'relative',
            'bottom' => 50,
            'right' => 50
        ];
        $video->filters()->watermark($watermarkPath, $relative);
        // $video->save(new X264('libfdk_aac'), $newPath);
        $video->save(new X264('libfdk_aac'), $this->newPath);
        return $this->newPath;
    }
    //截取视频、音频
    public function cutVideo($videoPath,$duration=10){
        $video = $this->ffmpeg->open($videoPath);
        $video->filters()->clip(TimeCode::fromSeconds(10),$duration);
        $video->save(new X264('libfdk_aac'), $this->newPath);
        return $this->newPath;
    }
    //录制rtmp为mp4
    public function recordVideo($rtmp,$duration=10){
        // dump($rtmp);exit;
        $time = secToTime($duration);
        $command = "ffmpeg -y -i  \"{$rtmp}\" -t {$time} -vcodec copy -acodec copy -f mp4 {$this->newPath}";
        exec($command);
        return $this->newPath;
    }
}
